﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ReadFileText
{
    public partial class Form1 : Form
    {
        internal System.Windows.Forms.SaveFileDialog SaveFileDialog = new System.Windows.Forms.SaveFileDialog();
        const string TypeFileXLS = "File Text (*.txt)|*.txt|File Excel (*.xls)|*.xls";
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        //Hàm tách chuỗi thành mảng theo dấu hiệu cho trước
        private string[] splitText(string istrValues, char istrToken)
        {
            ArrayList varlStore = new ArrayList();
            int viNewPosition = -1, viOldPosition = 0;

            while (istrValues.IndexOf(istrToken, (viNewPosition + 1) > istrValues.Length ? istrValues.Length : (viNewPosition + 1)) >= 0)
            {
                viNewPosition = istrValues.IndexOf(istrToken, viNewPosition + 1);
                varlStore.Add(istrValues.Substring(viOldPosition, viNewPosition - viOldPosition));
                viOldPosition = viNewPosition = viNewPosition + 3;
            }
            string[] vstrTemp = new string[varlStore.Count];
            for (int i = 0; i < varlStore.Count; i++)
            {
                vstrTemp[i] = varlStore[i].ToString();
            }
            return vstrTemp;
        }
        //==========================================
        //Hàm đọc file
        private string[] readTextFile(string istrPath)
        {
            System.IO.StreamReader vsrText;
            string vstrTemp;
            try
            {
                vsrText = new System.IO.StreamReader(istrPath);
                vstrTemp = vsrText.ReadToEnd();
                vsrText.Close();
            }
            catch
            {
                return null;
            }

            return this.splitText(vstrTemp, Convert.ToChar((int)Keys.Enter));
        }
        //==========================================
        //Hàm hiển thị 
        private void listToGrid(string istrFilePath)
        {            
            string[] vstrValuesToDisplay = this.readTextFile(istrFilePath);
            for (int i = 0; i < vstrValuesToDisplay.Length; i++)
            {
                this.gridStockInfo.Rows.Add();
                string Test = vstrValuesToDisplay[i];
                string[] arr_end = this.GetArr(Test);
                int stt = i + 1;
                this.gridStockInfo[0, i].Value = stt.ToString();
                this.gridStockInfo[1, i].Value = arr_end[1].ToString();
                double soluong = double.Parse(arr_end[2].ToString());
                this.gridStockInfo[2, i].Value = soluong.ToString("###,###,###");
                string gia = arr_end[3].ToString();
                double giadat = double.Parse(gia) * 1000;
                this.gridStockInfo[3, i].Value = giadat.ToString("###,###,###");
                this.gridStockInfo[4, i].Value = arr_end[4].ToString();
                this.gridStockInfo[5, i].Value = arr_end[5].ToString();
                this.gridStockInfo[6, i].Value = arr_end[0].ToString().Substring(0, 3);
                this.gridStockInfo[7, i].Value = arr_end[0].ToString().Substring(3, 3);
            }                      
        }        
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                this.listToGrid(this.textBox1.Text);
                this.button2.Enabled = true;
            }
            catch
            {
                MessageBox.Show(this, "Error in the path file");
            }
        }        

        private string[] GetArr(string text)
        {
            string chuoi = text;
            chuoi = chuoi.Substring(chuoi.Length - 82, 82);
            string[] arr = chuoi.Split(' ');
            string[] arr_end = new string[6];
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] != "")
                {
                    if (arr_end[0] == null)
                    {
                        arr_end[0] = arr[i];
                    }
                    else
                    {
                        if (arr_end[1] == null)
                        {
                            arr_end[1] = arr[i];
                        }
                        else
                        {
                            if (arr_end[2] == null)
                            {
                                arr_end[2] = arr[i];
                            }
                        }
                    }
                }
                
            }
            arr_end[3] = chuoi.Substring(25, 34-25);

            arr_end[4] = chuoi.Substring(34, 10);
            arr_end[5] = chuoi.Substring(44, 10);
            return arr_end;
        }
        private string SaveFile(string sFilter, string sTitle)
        {
            SaveFileDialog.RestoreDirectory = true;
            SaveFileDialog.Filter = sFilter;
            if (SaveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string fileName = SaveFileDialog.FileName;
                return fileName;
            }
            else
            {
                return string.Empty;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if ((this.gridStockInfo.Rows.Count > 0))
                {
                    if (MessageBox.Show("Bạn có muốn kiết suất kết quả ra file không?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        string sfileKQ = this.SaveFile(TypeFileXLS, "Save as ... ");
                        if (sfileKQ != string.Empty)
                        {
                            bool kq = new Class1().XuatKQTS2Excel(this.gridStockInfo, sfileKQ);
                            if (kq)
                            {
                                MessageBox.Show("Kiết xuất file Excell thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            else
                            {
                                MessageBox.Show("Kiết xuất file Excell thất bại !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            this.gridStockInfo.Rows.Clear();
                        }
                    }
                }               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.gridStockInfo.DataSource = null;
            this.textBox1.Text = "";
            string sfileKQ = this.SaveFile(TypeFileXLS, "Save as ... ");
            this.textBox1.Text = sfileKQ;
            this.button1.Enabled = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.gridStockInfo.Rows.Clear();
        }
    }
}